package com.iweb.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/*")
public class AdminServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("do admin");

        // 接收一个参数 id： id==1 我们执行 forward | id==2执行redirect
        String id = req.getParameter("id");
        //localhost:8090/admin?id=1
        if (id.equals("1")) {
            // 转发的方式
            req.getRequestDispatcher("/main.jsp").forward(req, resp);
        }
        //localhost:8090/admin?id=2
        if (id.equals("2")) {
            // 重定向： 重新发起一个请求
            resp.sendRedirect("/main.jsp");
        }
        //localhost:8090/admin?id=3
        if (id.equals("3")) {
            // 重新发了一个请求
            resp.sendRedirect("/user");
        }

    }
}
